# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem
import os
import shutil

class SpPipeline(object):
    def process_item(self, item, spider):
        import sys
        reload(sys)
        sys.setdefaultencoding('utf-8')
        
        _store_path = spider.settings.get('IMAGES_STORE')        
        _store_path = os.path.abspath(_store_path)
        _img_path = os.path.join(_store_path, item['postid'])
        if not os.path.isdir(_img_path):
            os.mkdir(_img_path, 0664)
        for i in item['images']:
            _filename = os.path.basename(i)
            _dst_path = os.path.join(_img_path, _filename)
            _src_path = os.path.join(_store_path, i)
            try:
                shutil.move(_src_path, _dst_path)
            except Exception, e:
                pass
                
        _desc_file = os.path.join(_img_path, 'desc.txt')
        with open(_desc_file, 'w') as f :
            _link_text = '\n'.join(item['image_urls'])
            f.write("%s\n\n%s\n\n%s"%(item['title'], item['link'], _link_text))
        return item
        
        
class StoreImgPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            if image_url:
                yield scrapy.Request(image_url)
    
    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
            
        item['images'] = image_paths
        return item